import { AudioClip, AssetManager, Material } from 'cc';

export interface i18n {
    _language: string;
    ready: boolean;
    t (key: string): string;
}

export interface IAssetBundle {
    getBundle(): AssetManager.Bundle;
    onReleaseCallback(callback: ()=>void): void;
}

export interface IDevice {
    i18n : i18n;

    graphicsMaterial?: Material;

    playOneShotSound(clip: AudioClip, volumeScale?: number): void;
    
    loadBundle(bundleName: string): Promise<IAssetBundle>;
}

class DefaultI18nImp implements i18n {
    _language: string = 'zh';
    ready: boolean = true;
    t(key: string): string {
        return key;
    }
}

export class UIDevice implements IDevice {
    private static _inited: boolean;
    private static _instance: UIDevice;
    public static get inst(): UIDevice {
        if (!this._instance) {
            this._instance = new UIDevice();
        }
        return this._instance;
    }

    public static get inited(): boolean {
        return this._inited;
    }

    get graphicsMaterial(): Material {
        return this._deviceInst.graphicsMaterial;
    }

    public static InitUIDeivce(device: IDevice) {
        this._inited = true;
        device.i18n = device.i18n || new DefaultI18nImp();
        this.inst._deviceInst = device;
    }

    public get i18n(): i18n {
        return this._deviceInst.i18n;
    } 

    private _deviceInst: IDevice;
    private _bundleReleaseCallback: () => void;
    
    playOneShotSound(clip: AudioClip, volumeScale?: number): void {
        this._deviceInst?.playOneShotSound(clip, volumeScale);
    }

    loadBundle(bundleName: string): Promise<IAssetBundle> {
        if (!this._deviceInst) {
            return new Promise((resolve, reject) => {
                reject('not init');
            });
        }

        return this._deviceInst.loadBundle(bundleName);
    }
}